package admin_api

import (
	"github.com/gin-gonic/gin"
	"shop/admin/admin_service"
	"shop/config"
	"shop/entity"
	"shop/utils/convertutil"
	"shop/utils/result"
)

func GetRoleList(c *gin.Context) {
	result.RenderResult(c, admin_service.GetRoleList())
}

func AddRole(c *gin.Context) {
	role := new(entity.SysRole)
	if err := c.ShouldBindJSON(role); err != nil {
		result.RenderFail(c, "参数格式错误："+err.Error())
		return
	}

	result.RenderResult(c, admin_service.AddRole(role))
}

func UpdateRole(c *gin.Context) {
	role := new(entity.SysRoleFilter)
	if err := c.ShouldBindJSON(role); err != nil {
		result.RenderFail(c, "参数格式错误："+err.Error())
		return
	}

	result.RenderResult(c, admin_service.UpdateRole(convertutil.ParseInt(c.Param("id")), role))
}

func DeleteRole(c *gin.Context) {
	id := convertutil.ParseInt(c.Param("id"))
	result.RenderResult(c, admin_service.DeleteRole(id))
}

func GetPermissionTree(c *gin.Context) {
	result.RenderResult(c, result.Ok(config.PermissionTree))
}
